package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Toast;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class VideoActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, SurfaceHolder.Callback {
    private SurfaceView sv;
    private SurfaceHolder holder;
    private MediaPlayer mediaPlayer;
    private RelativeLayout rl;
    private Timer timer;
    private TimerTask task;
    private SeekBar sbar;
    private ImageView play;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_video);
        sv = (SurfaceView) findViewById(R.id.sv);
        holder = sv.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(this);
        rl = (RelativeLayout) findViewById(R.id.rl);
        play = (ImageView) findViewById(R.id.play);
        sbar = (SeekBar) findViewById(R.id.sbar);
        sbar.setOnSeekBarChangeListener(this);
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                if(mediaPlayer != null && mediaPlayer.isPlaying()) {
                    int total = mediaPlayer.getDuration();
                    sbar.setMax(total);
                    int progress = mediaPlayer.getCurrentPosition();
                    sbar.setProgress(progress);
                }else {
                    play.setImageResource(android.R.drawable.ic_media_play);
                }
            }
        };
        timer.schedule(task, 500, 500);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder){
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            Uri uri = Uri.parse("https://www.ywicc.edu.cn/__local/C/EE/96/9B71501497395621789C73A0749_CFC1E89E_17A8E4BD.mp4");
            try {
                mediaPlayer.setDataSource(VideoActivity.this, uri);
            } catch (IOException e) {
                Toast.makeText(VideoActivity.this, "播放失败", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
            mediaPlayer.setDisplay(holder);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        }catch (Exception e) {
            Toast.makeText(VideoActivity.this, "播放失败", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if(mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
    }
    public void click(View view) {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            play.setImageResource(android.R.drawable.ic_media_play);
        }else {
            mediaPlayer.start();
            play.setImageResource(android.R.drawable.ic_media_pause);
        }
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar){
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int position = seekBar.getProgress();
        if (mediaPlayer != null) {
            mediaPlayer.seekTo(position);
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (rl.getVisibility() == View.INVISIBLE) {
                    rl.setVisibility(View.VISIBLE);
                    CountDownTimer cdt = new CountDownTimer(3000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            System.out.println(millisUntilFinished);
                        }

                        @Override
                        public void onFinish() {
                            rl.setVisibility(View.INVISIBLE);
                        }
                    };
                    cdt.start();
                }else if (rl.getVisibility() == View.VISIBLE) {
                    rl.setVisibility(View.VISIBLE);
                }
                break;
        }
        return super.onTouchEvent(event);
    }
    @Override
    protected void onDestroy() {
        task.cancel();
        timer.cancel();
        timer = null;
        task = null;
        mediaPlayer.release();
        mediaPlayer = null;
        super.onDestroy();
    }

}